﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class CourseInsert : System.Web.UI.Page
    {
        protected string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // 采用直接执行SQL语句的方法实现添加功能
        protected void btnOK_Click(object sender, EventArgs e)    // 添加
        {
            if (Page.IsValid)
            {
                string Cno = txtCno.Text.Trim().Replace("'", "''");
                string Cname = txtCname.Text.Trim().Replace("'", "''");
                string Lecture = txtLecture.Text.Trim().Replace("'", "''");
                string Semester = txtSemester.Text.Trim().Replace("'", "''");
                string Credit = txtCredit.Text.Trim().Replace("'", "''");


                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = string.Format("INSERT course(cno, cname, lecture, semester, credit) VALUES('{0}', '{1}', '{2}', '{3}', '{4}')", Cno, Cname, Lecture, Semester, Credit);
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();                            // 执行命令
                Response.Redirect("CourseManage.aspx?item=1");         // 返回到主页面
            }
        }

        

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtCno.Text = "";
            txtCname.Text = "";
            txtLecture.Text = "";
            txtSemester.Text = "";
            txtCredit.Text = "";
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string Cno = txtCno.Text.Trim().Replace("'", "''");

            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("SELECT * FROM course WHERE cno ='{0}'", Cno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataReader dr = myCommand.ExecuteReader();
            args.IsValid = !dr.Read();

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CourseManage.aspx?item=1");
        }
    }
}